<template>
  <el-dialog
    :visible.sync="dialogVisible"
    title="请选择驳回到哪个流程"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :before-close="beforeClose"
    class="tag-edit-dialog tag-reject-dialog"
    @closed="clearDialog"
    width="25%"
    v-dialogMove
  >
    <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
      <el-form-item label="驳回流程" prop="nodeId">
        <el-select v-model="editForm.nodeId">
          <el-option v-for="item in process" :key="item.nodeId" :label="item.nodeNm" :value="item.nodeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="驳回意见" prop="handlerCmnt">
        <el-input
          placeholder="请输入驳回意见"
          v-model="editForm.handlerCmnt"
          type="textarea"
          :maxlength="500"
          :rows="5"
          :show-word-limit="true"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="beforeClose()">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    procId: {
      // 1、需求；2、注册
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      dialogVisible: false,
      process: [],
      isChange: false,
      editForm: {
        tagId: '',
        nodeId: '',
        handlerCmnt: ''
      },
      editFormRules: {
        nodeId: [{ required: true, message: '流程不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    showDialog(tagId, isChange = false) {
      this.editForm.tagId = tagId
      this.isChange = isChange
      this.getList()
    },
    getList() {
      let url = this.isChange
        ? `ast/astTagRequireModify/listSeptBackProc/${this.editForm.tagId}`
        : `ast/astTagRequireManager/listSeptBackProc/${this.editForm.tagId}/${this.procId}`
      this.$ajax({
        url: this.$ajax.setUrl(url),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.process = data.data
          this.dialogVisible = true
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    submit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          let url = this.isChange ? 'ast/astTagRequireModify/septBackProc' : 'ast/astTagRequireManager/septBackProc'
          this.$ajax({
            url: this.$ajax.setUrl(url),
            method: 'post',
            data: this.$ajax.setData({
              handlerCmnt: this.editForm.handlerCmnt,
              tagId: this.editForm.tagId,
              procNodeId: this.editForm.nodeId,
              procId: this.procId
            })
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('callback')
              this.editForm.nodeId = ''
              this.editForm.handlerCmnt = ''
              this.dialogVisible = false
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    beforeClose() {
      this.dialogVisible = false
    },
    clearDialog() {
      this.editForm.nodeId = ''
      this.isChange = false
      this.$nextTick(() => this.$refs.editForm.clearValidate())
    }
  }
}
</script>

<style lang="scss">
.tag-reject-dialog {
  .el-form {
    margin-top: 20px;
  }
}
</style>
